package com.jogern.baseadapter.recycler;

import android.support.v7.widget.RecyclerView;
import android.view.View;

public abstract class RecyclerTypeHolder<T> {
    protected ViewHolder mViewHolder;
    private BaseRecyclerAdapter<T> mAdapter;
    private RecyclerView mRecyclerView;

    public RecyclerTypeHolder(BaseRecyclerAdapter<T> adapter, RecyclerView recyclerView) {
        mAdapter = adapter;
        mRecyclerView = recyclerView;
    }

    protected abstract void onBind(ViewHolder holder, T t, int position);

    /** 加载布局的LayoutId */
    public abstract int getLayoutId();

    ViewHolder createHolder(View view) {
        mViewHolder = new ViewHolder(view);
        initHolderView(view);
        return mViewHolder;
    }

    private RecyclerTypeHolder<T> typeHolder() {
        return this;
    }

    protected BaseRecyclerAdapter<T> getAdapter() {
        return mAdapter;
    }

    protected RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    protected abstract void initHolderView(View view);

    public final class ViewHolder extends RecyclerView.ViewHolder {

        ViewHolder(View itemView) {
            super(itemView);
        }

        void onBindHolder(ViewHolder holder, int position) {
            onBind(holder, mAdapter.getItem(position), position);
        }

        public RecyclerTypeHolder<T> getTypeHolder() {
            return typeHolder();
        }
    }
}